<?php
function code(){  
$REDIRECT_URI='';
$scope='snsapi_base'; //静默授权 关注用户默认的权限
//$scope='snsapi_userinfo'; //用户点击授权  未关注也可以获取
$url='http://open.weixin.qq.com/connect/oauth2/authorize?appid='.APPID.'&redirect_uri='.urlencode($REDIRECT_URI).'&response_type=code&scope='.$scope.'&state=123#wechat_redirect';
$recode= file_get_contents($url);


 header("Location:".$url);
}
//获取access_token
function access_token(){	
		$APPID=C('APPID');
		$APPSECRET=C('APPSECRET');
		$token=M('token');
		$require=$token->find();
		$id=$require['id'];
		$access_token=$require['token'];
		$ctime=$require['ctime'];
		$time=time();
		$newtime=$ctime+7000;	
		if(empty($access_token)){
		 
			$token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" .$APPID. "&secret=" .$APPSECRET;
			$res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
			//print_r($res);exit('123');
			$result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
			$access_token = $result['access_token']; 
			//print_r($access_token);exit('111');
			$data=array('token'=>$access_token,'ctime'=>$time,);
			$re=$token->add($data);	
			if(!$re){
				echo "参数错误1";		
			}
		
		}
		if(!empty($ctime) && $time>=$newtime){				
			$token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" .$APPID. "&secret=" .$APPSECRET;
			$res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
			//echo $res;
			$result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
			$access_token = $result['access_token'];
			$data=array('token'=>$access_token,'ctime'=>$time,);
			$re=$token->where('id='.$id)->save($data);		
			if(!$re){
				echo "参数错误2";	
				}		
			}//print_r($access_token);exit('111');
			return $access_token;
	}
  
    function getJsApiTicket() {
    // jsapi_ticket 应该全局存储与更新，以下代码以写入到文件中做示例
   
      $accessToken =access_token();
      //print_r($accessToken);exit;
      // 如果是企业号用以下 URL 获取 ticket
      // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
     /* $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$accessToken."&type=jsapi";
      $res = file_get_contents($url);
      $result=json_decode($res,true);
      $ticket =$result['ticket'];
*/
    	$ticket=M('ticket');
		$require=$ticket->find();
		$id=$require['id'];
		$tic=$require['ticket'];
		$ctime=$require['ctime'];
		$time=time();
		$newtime=$ctime+7000;	
		if(empty($tic)){
		 
			 $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$accessToken."&type=jsapi";
		      $res = file_get_contents($url);
		      $result=json_decode($res,true);
		      $tic =$result['ticket'];
			//print_r($access_token);exit('111');
			$data=array('ticket'=>$tic,'ctime'=>$time,);
			$re=$ticket->add($data);	
			if(!$re){
				echo "参数错误1";		
			}
		
		}
		if(!empty($ctime) && $time>=$newtime){				
			 $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$accessToken."&type=jsapi";
		      $res = file_get_contents($url);
		      $result=json_decode($res,true);
		      $tic =$result['ticket'];
			$data=array('ticket'=>$tic,'ctime'=>$time,);
			$re=$ticket->where('id='.$id)->save($data);		
			if(!$re){
				echo "参数错误2";	
				}		
			}//print_r($tic);exit('111');
			return $tic;
  }

  	//
 function getSignPackage() {
    $jsapiTicket =getJsApiTicket();

    // 注意 URL 一定要动态获取，不能 hardcode.
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

    $timestamp = time();
    $nonceStr =createNonceStr();
    // 这里参数的顺序要按照 key 值 ASCII 码升序排序
    $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";

    $signature = sha1($string);
    //print_r($signature);exit('2');

    $signPackage = array(
      "appId"     => C('APPID'),
      "nonceStr"  => $nonceStr,
      "timestamp" => $timestamp,
      "url"       => $url,
      "signature" => $signature,
      "rawString" => $string
    );
    // print_r($signPackage);exit('@@@');
    return $signPackage; 
  }

  function createNonceStr($length = 16) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $str = "";
    for ($i = 0; $i < $length; $i++) {
      $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
  }
   

?>